package com.sjb.cp.manager.auth;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.sjb.cp.model.enums.SpaceRoleEnum;
import com.sjb.cp.model.enums.SpaceTypeEnum;
import com.sjb.cp.model.po.Space;
import com.sjb.cp.model.po.SpaceUser;
import com.sjb.cp.model.po.User;
import com.sjb.cp.service.SpaceUserService;
import com.sjb.cp.service.UserService;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.swing.plaf.ProgressBarUI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Author:sjb
 * @CreateTime:2025-02-14
 * @Description: 空间权限管理
 * @Version：1.0
 */
@Component
public class SpaceUserAuthManager {


    private static final SpaceUserAuthConfig SPACE_USER_AUTH_CONFIG;

    @Resource
    private SpaceUserService spaceUserService;

    @Resource
    private UserService userService;

    static {
        String json = ResourceUtil.readUtf8Str("biz/spaceUserAuthConfig.json");
        SPACE_USER_AUTH_CONFIG = JSONUtil.toBean(json, SpaceUserAuthConfig.class);
    }

    /**
     * 根据角色获取对应权限列表
     * @param spaceUserRole
     * @return
     */
    public List<String> getPermissionList(String spaceUserRole) {
        if (StrUtil.isEmpty(spaceUserRole)) {
            return Collections.emptyList();
        }

        SpaceUserRole roleS = SPACE_USER_AUTH_CONFIG.getSpaceUserRoles().stream()
                .filter(result -> spaceUserRole.equals(result.getKey()))
                .findFirst().orElse(null);

        if (roleS == null) {
            return Collections.emptyList();
        }
        return roleS.getPermissions();
    }

    /**
     * 获取权限列表
     * @param space
     * @param loginUser
     * @return
     */
    public List<String> getPermissionsList(Space space, User loginUser){
        // 公共图库、私有空间、团队空间
        if (loginUser == null){
            return Collections.emptyList();
        }

        List<String> adminPermissionS = getPermissionList(SpaceRoleEnum.ADMIN.getValue());
        // 公共图库
        if (space == null){
            if (userService.isAdmin(loginUser)){
                return adminPermissionS;
            }
            return new ArrayList<>();
        }
        // 根据空间类型 判断权限
        SpaceTypeEnum typeEnumByValue = SpaceTypeEnum.getTypeEnumByValue(space.getSpaceType());
        switch (Objects.requireNonNull(typeEnumByValue)) {
            case PRIVATE:
                // 管理员权限
                if (space.getUserId().equals(loginUser.getId()) || userService.isAdmin(loginUser)){
                    return adminPermissionS;
                }else{
                    return Collections.emptyList();
                }
            case TEAM:
                // 团队空间 根据spaceUser获取角色或权限
                SpaceUser spaceUser = spaceUserService.lambdaQuery()
                        .eq(SpaceUser::getUserId, loginUser.getId())
                        .eq(SpaceUser::getSpaceId, space.getId())
                        .one();
                if (spaceUser == null){
                    return Collections.emptyList();
                }
                String spaceRole = spaceUser.getSpaceRole();
                return getPermissionList(spaceRole);
        }
        return Collections.emptyList();
    }
}
